package com.ccgk.logistics.service.impl;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.ccgk.common.page.PaginationVo;
import com.ccgk.logistics.domain.Container;
import com.ccgk.logistics.domain.Port;
import com.ccgk.logistics.domain.PortExample;
import com.ccgk.logistics.persistence.PortMapper;
import com.ccgk.logistics.service.PortService;

@Service
public class PortServiceImpl implements PortService{
	
	@Autowired
	private PortMapper portMapper;
	
	@Override
	public Integer add(Port port) {
		return portMapper.insert(port);
	}

	@Override
	public void batchDelete(Integer[] idIntArray) {
		for(Integer id:idIntArray){
			delete(id);
		}
	}

	@Override
	public Integer delete(int id) {
		return portMapper.deleteByPrimaryKey(id);
	}

	@Override
	public List<Port> findAll() {
		return portMapper.selectByExample(null);
	}

	@Override
	public PaginationVo<Port> findList(int currentPage, int pageSize) {
		PortExample example = new PortExample();
		List<Port> data = portMapper.selectByExample(example,new RowBounds((currentPage-1)*pageSize, pageSize));
		int recordCount = portMapper.countByExample(example);
		PaginationVo<Port> result = new PaginationVo<Port>(data, recordCount, pageSize, currentPage);
		return result;
	}

	@Override
	public Port getById(int id) {
		return portMapper.selectByPrimaryKey(id);
	}

	@Override
	public Integer update(Port port) {
		return portMapper.updateByPrimaryKey(port);
	}

	@Override
	public Port getByName(String name) {
		PortExample example = new PortExample();
		example.createCriteria().andNameLike(name);
		List<Port> result = portMapper.selectByExample(example);
		if(CollectionUtils.isEmpty(result))
			return null;
		return result.get(0);
	}

	@Override
	public List<Port> findByName(String name) {
		PortExample example = new PortExample();
		example.createCriteria().andNameLikeBlear(name);
		List<Port> result = portMapper.selectByExample(example);
		return result;
	}

}
